<?php
/**
 * Created by PhpStorm.
 * User: hhlov_000
 * Date: 15-3-25
 * Time: 上午2:45
 */

namespace Admin\Controller;


class KeywordController extends CommonController{
    public function keywordList($page = 1, $rows = 10, $sort = 'id', $order = 'asc'){
        if(IS_POST){
            $keyword_db = M('keywords');
            $total = $keyword_db->count();
            $order = $sort.' '.$order;
            $limit = ($page - 1) * $rows . "," . $rows;
            //$list = $member_db->table(C('DB_PREFIX').'user M')->join(C('DB_PREFIX').'member_type MT on MT.typeid = M.typeid')->field("M.memberid,M.user_name,M.sex")->order($order)->limit($limit)->select();
            //$list = $member_db->field('user_id,user_name,sex,phone,status')->order($order)->limit($limit)->select();
            $list = $keyword_db
                ->field(array('id','keyword','order','status'))
                ->order($order)->limit($limit)->select();
            if(!$list) $list = array();
            $data = array('total'=>$total, 'rows'=>$list);
            $this->ajaxReturn($data);
        }else{
            $menu_db = D('Menu');
            $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
            $datagrid = array(
                'options'     => array(
                    'title'   => $currentpos,
                    'url'     => U('Keyword/keywordList', array('grid'=>'datagrid')),
                    'toolbar' => 'keyword_keywordlist_datagrid_toolbar',
                ),
                'fields' => array(
                    '关键词'      => array('field'=>'keyword','width'=>15,'sortable'=>true),
                    '顺序'    => array('field'=>'order','width'=>15,'sortable'=>true),
                    '状态' => array('field'=>'status','width'=>8,'sortable'=>true,'formatter'=>'keywordStatusFormatter'),
                    '管理操作'    => array('field'=>'id','width'=>25,'formatter'=>'keywordOperateFormatter'),
                )
            );

            $this->assign('datagrid', $datagrid);
            $this->display('keyword_list');
        }

    }

    public function keywordEdit($id){
        if(IS_POST){
            $data['keyword'] = $_POST['keyword'];
            $data['id'] = $_POST['id'];
            $data['order'] = $_POST['order'];
            $keyword_db = M('keywords');
            $keyword_db->save($data);

            echo '提交成功';

        }else{
            $keyword_db = M('keywords');
            $list = $keyword_db->where("id=%d",$id)->select();
            if(sizeof($list)==1){
                $this->assign('keyword',$list[0]);
                $this->display('keyword_edit');
            }else{
                $this->error('异常操作');
            }

        }
    }
    public function keywordDelete($id){
        $keyword_db = M('keywords');
        $result = $keyword_db->where(array('id'=>$id))->delete();
        if ($result){
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }
    public function keywordAdd(){
        if(IS_POST){
            $keyword_db = M('keywords');
            $max= $keyword_db->max('keywords.order');
            $data['keyword'] = $_POST['keyword'];
            $data['status'] = 1;
            $data['order'] = $max+1;

            $keyword_db->add($data);

            echo '提交成功';

        }else{

            $this->display('keyword_add');

        }

    }
} 